﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using qip_history_synchronizer.Mimes;
using qip_history_synchronizer.Pop3;

namespace qip_history_synchronizer
{
    class Program
    {
        static void Main( string[] args )
        {
            string path = @"C:\Program Files\QIP";
            List<string> arrHistFiles = HistoryUtils.getQIPHistoryFileNames( path, "364605811" );
            Hashtable htContents = HistoryUtils.getFilesContent( arrHistFiles );
            foreach( DictionaryEntry fileContent in htContents )
            {
                string file = ( string ) fileContent.Key;
                List<string> contentLines = ( List<string> ) fileContent.Value;
                List<Message> fileMessages = HistoryUtils.getHistoryOrderlyList( contentLines );
                string strContent = HistoryUtils.getContentFromLines( contentLines );

                Hashtable htMailMessagesContents = Pop3Utils.getMessagesWithContents();
                bool contains = htMailMessagesContents.Contains( Path.GetFileNameWithoutExtension( file ) );
                if( contains )
                {
                    string mailMessageContant = ( string ) htMailMessagesContents[Path.GetFileNameWithoutExtension( file )];
                    List<string> mailMessageContantList = HistoryUtils.getContantInList( mailMessageContant );
                    List<Message> mailMessages = HistoryUtils.getHistoryOrderlyList( contentLines );
                    List<Message> synchronizedMessages = HistoryUtils.synchronize( fileMessages, mailMessages );

                    bool isEquals = HistoryUtils.CompareMessagesLists( fileMessages, synchronizedMessages );
                    if( !isEquals )
                    {
                        string synchronizedContent = HistoryUtils.getContentFromList( synchronizedMessages );
                        MailUtils.sendEmail( Path.GetFileNameWithoutExtension( file ), synchronizedContent );
                    }
                }
                else
                {
                    MailUtils.sendEmail( Path.GetFileNameWithoutExtension( file ), strContent );
                }
            }
        }
       
    }
}
